package role

import (
	"gohub/pkg/app"
	"gohub/pkg/database"
	"gohub/pkg/paginator"
	"strings"

	"github.com/gin-gonic/gin"
)

func GetRoles(roleid string) (Roles []Role) {
	database.DB.Where("id IN (?)", strings.Split(roleid, ",")).Find(&Roles)
	return
}

// Get 通过 ID 获取用户
func Get(idstr string) (RoleModel Role) {
	database.DB.Where("id", idstr).First(&RoleModel)
	return
}

// All 获取所有用户数据
func All() (Roles []Role) {
	database.DB.Find(&Roles)
	return
}

// Paginate 分页
func Paginate(c *gin.Context, perPage int) (roles []Role, paging paginator.Paging) {
	paginator := paginator.New(c, database.DB.Model(&Role{}), app.V1URL(database.TableName(&Role{})))
	paging = paginator.Paginate(&roles)
	return
}

// func Paginate(c *gin.Context, perPage int) (roles []Role, paging paginator.Paging) {
// 	paging = paginator.Paginate(
// 		c,
// 		database.DB.Model(Role{}),
// 		&roles,
// 		app.V1URL(database.TableName(&Role{})),
// 		perPage,
// 	)
// 	return
// }

func GetRoleIdByName(name string) (string, error) {
	var role Role
	result := database.DB.Where("name = ?", name).First(&role)
	if result.Error != nil {
		return "", result.Error
	}
	return role.GetStringID(), nil
}
